import { reactive } from 'vue'
import { updateTaskData, addTaskData, getTaskDataForm } from '@/api/task/task-piblish'
import { getOptionList } from '@/api/base/brand'
import { getWarehouseOptionList } from '@/api/base/warehouse'
import { getTageOptionLList } from '@/api/base/tag'
import { getStoreOptionsList } from '@/api/base/shop'

export function useDetail(getFormValues: Function) {
  const status = reactive({
    saving: false,
    loading: false
  })

  const createOrUpdate = async () => {
    const values = getFormValues()
    if (status.saving) return false
    status.saving = true
    try {
      values?.code ? await updateTaskData(values.code, values) : await addTaskData(values)
      status.saving = false
      return true
    } catch (err) {
      status.saving = false
      return false
    }
  }

  // 获取采购单列表
  const getDataById = async (userCode: any) => {
    return getTaskDataForm(userCode)
  }

  // 获取品牌方列表
  const getBrandOptions = async (type: any) => {
    return getOptionList(type)
  }

  // 获取仓库列表
  const getWarehouseList = async () => {
    return getWarehouseOptionList()
  }

  // 获取门店列表
  const getStoreOptions = async () => {
    return getStoreOptionsList()
  }

  // 获取标签列表
  const getTagList = async () => {
    return getTageOptionLList()
  }

  return {
    status,
    createOrUpdate,
    getDataById,
    getBrandOptions,
    getWarehouseList,
    getStoreOptions,
    getTagList
  }
}
